home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lib-src / yow.c < prev   
C/C++ Source or Header  |  1993-08-12  |  3KB  |  149 lines

  1. /*
  2.  * yow.c
  3.  * 
  4.  * Print a quotation from Zippy the Pinhead.
  5.  * Qux <Kaufman-David@Yale> March 6, 1986
  6.  * 
  7.  * With dynamic memory allocation.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include "../src/paths.h"      /* For PATH_DATA.  */
  13. #ifdef WINDOWSNT
  14. #include <process.h>
  15. #endif /* WINDOWSNT */
  16.  
  17. #define BUFSIZE  80
  18. #define SEP      '\0'
  19.  
  20. #ifndef YOW_FILE
  21. #define YOW_FILE "yow.lines"
  22. #endif
  23.  
  24. main (argc, argv)
  25.      int argc;
  26.      char *argv[];
  27. {
  28.   FILE *fp;
  29.   char file[BUFSIZ];
  30.   void yow(), setup_yow();
  31.  
  32.   if (argc > 2 && !strcmp (argv[1], "-f"))
  33.     strcpy (file, argv[2]);
  34.   else
  35. #ifdef vms
  36.     sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
  37. #else
  38.     sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
  39. #endif
  40.  
  41.   if ((fp = fopen(file, "r")) == NULL) {
  42.     perror(file);
  43.     exit(1);
  44.   }
  45.  
  46.   /* initialize random seed */
  47.   srand((int) (getpid() + time((long *) 0)));
  48.  
  49.   setup_yow(fp);
  50.   yow(fp);
  51.   fclose(fp);
  52.   exit(0);
  53. }
  54.  
  55. static long len = -1;
  56. static long header_len;
  57.  
  58. #define AVG_LEN 40        /* average length of a quotation */
  59.  
  60. /* Sets len and header_len */
  61. void
  62. setup_yow(fp)
  63.      FILE *fp;
  64. {
  65.   int c;
  66.  
  67.   /* Get length of file */
  68.   /* Because the header (stuff before the first SEP) can be very long,
  69.    * thus biasing our search in favor of the first quotation in the file,
  70.    * we explicitly skip that. */
  71.   while ((c = getc(fp)) != SEP) {
  72.     if (c == EOF) {
  73.       fprintf(stderr, "File contains no separators.\n");
  74.       exit(2);
  75.     }
  76.   }
  77.   header_len = ftell(fp);
  78.   if (header_len > AVG_LEN)
  79.     header_len -= AVG_LEN;    /* allow the first quotation to appear */
  80.     
  81.   if (fseek(fp, 0L, 2) == -1) {
  82.     perror("fseek 1");
  83.     exit(1);
  84.   }
  85.   len = ftell(fp) - header_len;
  86. }
  87.  
  88.  
  89. /* go to a random place in the file and print the quotation there */
  90. void
  91. yow (fp)
  92.      FILE *fp;
  93. {
  94.   long offset;
  95.   int c, i = 0;
  96.   char *buf;
  97.   unsigned int bufsize;
  98.   char *malloc(), *realloc();
  99.  
  100.   offset = rand() % len + header_len;
  101.   if (fseek(fp, offset, 0) == -1) {
  102.     perror("fseek 2");
  103.     exit(1);
  104.   }
  105.  
  106.   /* Read until SEP, read next line, print it.
  107.      (Note that we will never print anything before the first separator.)
  108.      If we hit EOF looking for the first SEP, just recurse. */
  109.   while ((c = getc(fp)) != SEP)
  110.     if (c == EOF) {
  111.       yow(fp);
  112.       return;
  113.     }
  114.  
  115.   /* Skip leading whitespace, then read in a quotation.
  116.      If we hit EOF before we find a non-whitespace char, recurse. */
  117.   while (isspace(c = getc(fp)))
  118.     ;
  119.   if (c == EOF) {
  120.     yow(fp);
  121.     return;
  122.   }
  123.  
  124.   bufsize = BUFSIZE;
  125.   buf = malloc(bufsize);
  126.   if (buf == (char *)0) {
  127.     fprintf(stderr, "can't allocate any memory\n");
  128.     exit (3);
  129.   }
  130.  
  131.   buf[i++] = c;
  132.   while ((c = getc(fp)) != SEP && c != EOF) {
  133.     buf[i++] = c;
  134.     
  135.     if (i == bufsize-1) {
  136.       /* Yow! Is this quotation too long yet? */
  137.       bufsize *= 2;
  138.       buf = realloc(buf, bufsize);
  139.       if (buf == (char *)0) {
  140.     fprintf(stderr, "can't allocate more memory\n");
  141.     exit (3);
  142.       }
  143.     }
  144.   }
  145.   buf[i++] = 0;
  146.   printf("%s\n", buf);
  147. }
  148.  
  149.